package c.e.a.b.f.b;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.text.TextUtils;
import b.f.a;
import b.i.b.f;
import c.e.a.b.b.j.d;
import c.e.a.b.e.c.e5;
import c.e.a.b.e.c.k0;
import c.e.a.b.e.c.l0;
import c.e.a.b.e.c.m0;
import c.e.a.b.e.c.o0;
import c.e.a.b.e.c.t0;
import c.e.a.b.e.c.u0;
import c.e.a.b.e.c.u4;
import c.e.a.b.e.c.u4.b;
import c.e.a.b.e.c.v0;
import c.e.a.b.e.c.z7;
import com.bytedance.embedapplog.GameReportHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/* compiled from: com.google.android.gms:play-services-measurement@@18.0.0 */
public final class p4 extends i9 implements e {
    public final Map<String, Map<String, String>> d = new a();
    public final Map<String, Map<String, Boolean>> e = new a();
    public final Map<String, Map<String, Boolean>> f = new a();
    public final Map<String, u0> g = new a();
    public final Map<String, Map<String, Integer>> h = new a();
    public final Map<String, String> i = new a();

    public p4(k9 k9Var) {
        super(k9Var);
    }

    public static Map<String, String> u(u0 u0Var) {
        a aVar = new a();
        for (v0 v0Var : u0Var.B()) {
            aVar.put(v0Var.u(), v0Var.v());
        }
        return aVar;
    }

    public final boolean A(String str) {
        g();
        u0 s = s(str);
        if (s == null) {
            return false;
        }
        return s.E();
    }

    public final long B(String str) {
        String c = c(str, "measurement.account.time_zone_offset_minutes");
        if (!TextUtils.isEmpty(c)) {
            try {
                return Long.parseLong(c);
            } catch (NumberFormatException e) {
                d().i.c("Unable to parse timezone offset. appId", q3.s(str), e);
            }
        }
        return 0;
    }

    public final boolean C(String str) {
        return "1".equals(c(str, "measurement.upload.blacklist_internal"));
    }

    public final boolean D(String str) {
        return "1".equals(c(str, "measurement.upload.blacklist_public"));
    }

    /* JADX WARNING: Removed duplicated region for block: B:26:0x00a1  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x0082  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x0082  */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x00a1  */
    /* JADX WARNING: Removed duplicated region for block: B:30:0x00d6  */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x007c  */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x00a1  */
    /* JADX WARNING: Removed duplicated region for block: B:24:0x0082  */
    public final void E(java.lang.String r12) {
        /*
        r11 = this;
        r11.n();
        r11.g();
        b.i.b.f.N(r12);
        r0 = r11.g;
        r0 = r0.get(r12);
        if (r0 != 0) goto L_0x00da;
    L_0x0011:
        r0 = r11.q();
        java.util.Objects.requireNonNull(r0);
        b.i.b.f.N(r12);
        r0.g();
        r0.n();
        r1 = 0;
        r2 = r0.t();	 Catch:{ SQLiteException -> 0x0068, all -> 0x0066 }
        r3 = "apps";
        r4 = "remote_config";
        r4 = new java.lang.String[]{r4};	 Catch:{ SQLiteException -> 0x0068, all -> 0x0066 }
        r5 = "app_id=?";
        r6 = 1;
        r6 = new java.lang.String[r6];	 Catch:{ SQLiteException -> 0x0068, all -> 0x0066 }
        r10 = 0;
        r6[r10] = r12;	 Catch:{ SQLiteException -> 0x0068, all -> 0x0066 }
        r7 = 0;
        r8 = 0;
        r9 = 0;
        r2 = r2.query(r3, r4, r5, r6, r7, r8, r9);	 Catch:{ SQLiteException -> 0x0068, all -> 0x0066 }
        r3 = r2.moveToFirst();	 Catch:{ SQLiteException -> 0x0064 }
        if (r3 != 0) goto L_0x0047;
    L_0x0043:
        r2.close();
        goto L_0x007f;
    L_0x0047:
        r3 = r2.getBlob(r10);	 Catch:{ SQLiteException -> 0x0064 }
        r4 = r2.moveToNext();	 Catch:{ SQLiteException -> 0x0064 }
        if (r4 == 0) goto L_0x0060;
    L_0x0051:
        r4 = r0.d();	 Catch:{ SQLiteException -> 0x0064 }
        r4 = r4.f;	 Catch:{ SQLiteException -> 0x0064 }
        r5 = "Got multiple records for app config, expected one. appId";
        r6 = c.e.a.b.f.b.q3.s(r12);	 Catch:{ SQLiteException -> 0x0064 }
        r4.b(r5, r6);	 Catch:{ SQLiteException -> 0x0064 }
    L_0x0060:
        r2.close();
        goto L_0x0080;
    L_0x0064:
        r3 = move-exception;
        goto L_0x006b;
    L_0x0066:
        r12 = move-exception;
        goto L_0x00d4;
    L_0x0068:
        r2 = move-exception;
        r3 = r2;
        r2 = r1;
    L_0x006b:
        r0 = r0.d();	 Catch:{ all -> 0x00d2 }
        r0 = r0.f;	 Catch:{ all -> 0x00d2 }
        r4 = "Error querying remote config. appId";
        r5 = c.e.a.b.f.b.q3.s(r12);	 Catch:{ all -> 0x00d2 }
        r0.c(r4, r5, r3);	 Catch:{ all -> 0x00d2 }
        if (r2 == 0) goto L_0x007f;
    L_0x007c:
        r2.close();
    L_0x007f:
        r3 = r1;
    L_0x0080:
        if (r3 != 0) goto L_0x00a1;
    L_0x0082:
        r0 = r11.d;
        r0.put(r12, r1);
        r0 = r11.e;
        r0.put(r12, r1);
        r0 = r11.f;
        r0.put(r12, r1);
        r0 = r11.g;
        r0.put(r12, r1);
        r0 = r11.i;
        r0.put(r12, r1);
        r0 = r11.h;
        r0.put(r12, r1);
        return;
    L_0x00a1:
        r0 = r11.t(r12, r3);
        r0 = r0.t();
        r0 = (c.e.a.b.e.c.u0.a) r0;
        r11.v(r12, r0);
        r2 = r11.d;
        r3 = r0.l();
        r3 = (c.e.a.b.e.c.u4) r3;
        r3 = (c.e.a.b.e.c.u0) r3;
        r3 = u(r3);
        r2.put(r12, r3);
        r2 = r11.g;
        r0 = r0.l();
        r0 = (c.e.a.b.e.c.u4) r0;
        r0 = (c.e.a.b.e.c.u0) r0;
        r2.put(r12, r0);
        r0 = r11.i;
        r0.put(r12, r1);
        goto L_0x00da;
    L_0x00d2:
        r12 = move-exception;
        r1 = r2;
    L_0x00d4:
        if (r1 == 0) goto L_0x00d9;
    L_0x00d6:
        r1.close();
    L_0x00d9:
        throw r12;
    L_0x00da:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.e.a.b.f.b.p4.E(java.lang.String):void");
    }

    public final String c(String str, String str2) {
        g();
        E(str);
        Map map = (Map) this.d.get(str);
        return map != null ? (String) map.get(str2) : null;
    }

    public final boolean p() {
        return false;
    }

    public final u0 s(String str) {
        n();
        g();
        f.N(str);
        E(str);
        return (u0) this.g.get(str);
    }

    public final u0 t(String str, byte[] bArr) {
        String str2 = "Unable to merge remote config. appId";
        if (bArr == null) {
            return u0.G();
        }
        try {
            u0 u0Var = (u0) ((u4) ((u0.a) q9.x(u0.F(), bArr)).l());
            s3 s3Var = d().n;
            String str3 = "Parsed config. version, gmp_app_id";
            Object obj = null;
            Object valueOf = u0Var.x() ? Long.valueOf(u0Var.y()) : null;
            if (u0Var.z()) {
                obj = u0Var.A();
            }
            s3Var.c(str3, valueOf, obj);
            return u0Var;
        } catch (e5 e) {
            d().i.c(str2, q3.s(str), e);
            return u0.G();
        } catch (RuntimeException e2) {
            d().i.c(str2, q3.s(str), e2);
            return u0.G();
        }
    }

    public final void v(String str, u0.a aVar) {
        a aVar2 = new a();
        a aVar3 = new a();
        a aVar4 = new a();
        for (int i = 0; i < ((u0) aVar.b).C(); i++) {
            t0.a aVar5 = (t0.a) ((u0) aVar.b).u(i).t();
            if (TextUtils.isEmpty(aVar5.m())) {
                d().i.a("EventConfig contained null event name");
            } else {
                String m = aVar5.m();
                String x = d.x(aVar5.m(), v5.a, v5.c);
                if (!TextUtils.isEmpty(x)) {
                    if (aVar5.c) {
                        aVar5.j();
                        aVar5.c = false;
                    }
                    t0.v((t0) aVar5.b, x);
                    if (aVar.c) {
                        aVar.j();
                        aVar.c = false;
                    }
                    u0.w((u0) aVar.b, i, (t0) ((u4) aVar5.l()));
                }
                if (z7.a() && this.a.g.o(p.N0)) {
                    aVar2.put(m, Boolean.valueOf(((t0) aVar5.b).w()));
                } else {
                    aVar2.put(aVar5.m(), Boolean.valueOf(((t0) aVar5.b).w()));
                }
                aVar3.put(aVar5.m(), Boolean.valueOf(((t0) aVar5.b).x()));
                if (((t0) aVar5.b).y()) {
                    if (aVar5.n() < 2 || aVar5.n() > 65535) {
                        d().i.c("Invalid sampling rate. Event name, sample rate", aVar5.m(), Integer.valueOf(aVar5.n()));
                    } else {
                        aVar4.put(aVar5.m(), Integer.valueOf(aVar5.n()));
                    }
                }
            }
        }
        this.e.put(str, aVar2);
        this.f.put(str, aVar3);
        this.h.put(str, aVar4);
    }

    public final boolean w(String str, byte[] bArr, String str2) {
        b bVar;
        String str3;
        String str4;
        String str5 = str;
        n();
        g();
        f.N(str);
        b bVar2 = (u0.a) t(str, bArr).t();
        v(str5, bVar2);
        this.g.put(str5, (u0) ((u4) bVar2.l()));
        this.i.put(str5, str2);
        this.d.put(str5, u((u0) ((u4) bVar2.l())));
        g q = q();
        ArrayList arrayList = new ArrayList(Collections.unmodifiableList(((u0) bVar2.b).D()));
        Objects.requireNonNull(q);
        String str6 = "app_id=? and audience_id=?";
        String str7 = "event_filters";
        String str8 = "app_id=?";
        String str9 = "property_filters";
        String str10 = "null reference";
        int i = 0;
        while (i < arrayList.size()) {
            k0.a aVar = (k0.a) ((k0) arrayList.get(i)).t();
            if (((k0) aVar.b).D() != 0) {
                int i2 = 0;
                while (i2 < ((k0) aVar.b).D()) {
                    Object obj;
                    b bVar3 = (l0.a) ((k0) aVar.b).z(i2).t();
                    l0.a aVar2 = (l0.a) ((b) bVar3.clone());
                    bVar = bVar2;
                    String x = d.x(((l0) bVar3.b).z(), v5.a, v5.c);
                    if (x != null) {
                        if (aVar2.c) {
                            aVar2.j();
                            aVar2.c = false;
                        }
                        l0.w((l0) aVar2.b, x);
                        obj = 1;
                    } else {
                        obj = null;
                    }
                    int i3 = 0;
                    while (i3 < ((l0) bVar3.b).B()) {
                        m0 u = ((l0) bVar3.b).u(i3);
                        b bVar4 = bVar3;
                        str3 = str6;
                        str4 = str10;
                        str6 = d.x(u.C(), u5.a, u5.b);
                        if (str6 != null) {
                            m0.a aVar3 = (m0.a) u.t();
                            if (aVar3.c) {
                                aVar3.j();
                                aVar3.c = false;
                            }
                            m0.u((m0) aVar3.b, str6);
                            m0 m0Var = (m0) ((u4) aVar3.l());
                            if (aVar2.c) {
                                aVar2.j();
                                aVar2.c = false;
                            }
                            l0.v((l0) aVar2.b, i3, m0Var);
                            obj = 1;
                        }
                        i3++;
                        bVar3 = bVar4;
                        str6 = str3;
                        str10 = str4;
                    }
                    str3 = str6;
                    str4 = str10;
                    if (obj != null) {
                        if (aVar.c) {
                            aVar.j();
                            aVar.c = false;
                        }
                        k0.v((k0) aVar.b, i2, (l0) ((u4) aVar2.l()));
                        arrayList.set(i, (k0) ((u4) aVar.l()));
                    }
                    i2++;
                    bVar2 = bVar;
                    str6 = str3;
                    str10 = str4;
                }
            }
            bVar = bVar2;
            str3 = str6;
            str4 = str10;
            if (((k0) aVar.b).B() != 0) {
                for (int i4 = 0; i4 < ((k0) aVar.b).B(); i4++) {
                    o0 u2 = ((k0) aVar.b).u(i4);
                    str6 = d.x(u2.x(), x5.a, x5.b);
                    if (str6 != null) {
                        boolean z;
                        o0.a aVar4 = (o0.a) u2.t();
                        if (aVar4.c) {
                            aVar4.j();
                            z = false;
                            aVar4.c = false;
                        } else {
                            z = false;
                        }
                        o0.u((o0) aVar4.b, str6);
                        if (aVar.c) {
                            aVar.j();
                            aVar.c = z;
                        }
                        k0.w((k0) aVar.b, i4, (o0) ((u4) aVar4.l()));
                        arrayList.set(i, (k0) ((u4) aVar.l()));
                    }
                }
            }
            i++;
            bVar2 = bVar;
            str6 = str3;
            str10 = str4;
        }
        bVar = bVar2;
        str3 = str6;
        str4 = str10;
        q.n();
        q.g();
        f.N(str);
        SQLiteDatabase t = q.t();
        t.beginTransaction();
        try {
            k0 k0Var;
            byte[] i5;
            q.n();
            q.g();
            f.N(str);
            SQLiteDatabase t2 = q.t();
            t2.delete(str9, str8, new String[]{str5});
            t2.delete(str7, str8, new String[]{str5});
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                k0Var = (k0) it.next();
                q.n();
                q.g();
                f.N(str);
                str8 = str4;
                Objects.requireNonNull(k0Var, str8);
                if (k0Var.x()) {
                    Object obj2;
                    String str11;
                    int y = k0Var.y();
                    for (l0 x2 : k0Var.C()) {
                        if (!x2.x()) {
                            q.d().i.c("Event filter with no ID. Audience definition ignored. appId, audienceId", q3.s(str), Integer.valueOf(y));
                            break;
                        }
                    }
                    for (o0 v : k0Var.A()) {
                        if (!v.v()) {
                            q.d().i.c("Property filter with no ID. Audience definition ignored. appId, audienceId", q3.s(str), Integer.valueOf(y));
                        }
                    }
                    for (l0 x22 : k0Var.C()) {
                        if (!q.O(str5, y, x22)) {
                            obj2 = null;
                            break;
                        }
                    }
                    obj2 = 1;
                    if (obj2 != null) {
                        for (o0 v2 : k0Var.A()) {
                            if (!q.P(str5, y, v2)) {
                                obj2 = null;
                                break;
                            }
                        }
                    }
                    if (obj2 == null) {
                        q.n();
                        q.g();
                        f.N(str);
                        SQLiteDatabase t3 = q.t();
                        str11 = str3;
                        t3.delete(str9, str11, new String[]{str5, String.valueOf(y)});
                        t3.delete(str7, str11, new String[]{str5, String.valueOf(y)});
                    } else {
                        str11 = str3;
                    }
                    str4 = str8;
                    str3 = str11;
                } else {
                    q.d().i.b("Audience with no ID. appId", q3.s(str));
                }
                str4 = str8;
            }
            ArrayList arrayList2 = new ArrayList();
            Iterator it2 = arrayList.iterator();
            while (it2.hasNext()) {
                k0Var = (k0) it2.next();
                arrayList2.add(k0Var.x() ? Integer.valueOf(k0Var.y()) : null);
            }
            q.W(str5, arrayList2);
            t.setTransactionSuccessful();
            t.endTransaction();
            b bVar5 = bVar;
            try {
                if (bVar5.c) {
                    bVar5.j();
                    bVar5.c = false;
                }
                u0.v((u0) bVar5.b);
                i5 = ((u0) ((u4) bVar5.l())).i();
            } catch (RuntimeException e) {
                d().i.c("Unable to serialize reduced-size config. Storing full config instead. appId", q3.s(str), e);
                i5 = bArr;
            }
            g q2 = q();
            f.N(str);
            q2.g();
            q2.n();
            ContentValues contentValues = new ContentValues();
            contentValues.put("remote_config", i5);
            try {
                if (((long) q2.t().update("apps", contentValues, "app_id = ?", new String[]{str5})) == 0) {
                    q2.d().f.b("Failed to update remote config (got 0). appId", q3.s(str));
                }
            } catch (SQLiteException e2) {
                q2.d().f.c("Error storing remote config. appId", q3.s(str), e2);
            }
            this.g.put(str5, (u0) ((u4) bVar5.l()));
            return true;
        } catch (Throwable th) {
            t.endTransaction();
        }
    }

    public final boolean x(String str, String str2) {
        g();
        E(str);
        String str3 = "1";
        if (str3.equals(c(str, "measurement.upload.blacklist_internal")) && t9.r0(str2)) {
            return true;
        }
        if (str3.equals(c(str, "measurement.upload.blacklist_public")) && t9.X(str2)) {
            return true;
        }
        Map map = (Map) this.e.get(str);
        if (map == null) {
            return false;
        }
        Boolean bool = (Boolean) map.get(str2);
        if (bool == null) {
            return false;
        }
        return bool.booleanValue();
    }

    public final boolean y(String str, String str2) {
        g();
        E(str);
        if ("ecommerce_purchase".equals(str2) || GameReportHelper.PURCHASE.equals(str2) || "refund".equals(str2)) {
            return true;
        }
        Map map = (Map) this.f.get(str);
        if (map == null) {
            return false;
        }
        Boolean bool = (Boolean) map.get(str2);
        if (bool == null) {
            return false;
        }
        return bool.booleanValue();
    }

    public final int z(String str, String str2) {
        g();
        E(str);
        Map map = (Map) this.h.get(str);
        if (map == null) {
            return 1;
        }
        Integer num = (Integer) map.get(str2);
        if (num == null) {
            return 1;
        }
        return num.intValue();
    }
}
